// pages/issue/issue.js

var api = require('../../../config/api.js');
var util = require('../../../utils/util');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    brand: {},
    elements: [
      { title: '发布动态', name: 'issueDaily', color: 'orange', icon: 'discover' },
      { title: '发布商品', name: 'issueGoods', color: 'green', icon: 'formfill' },
      { title: '我的店铺', name: 'myBrand', color: 'olive', icon: 'shop' },
      { title: '销售订单', name: 'brandOrder', color: 'red', icon: 'edit' },
    ],
  },

  toChild(e) {
    let that = this;
    let brand = that.data.brand;
    let type = e.currentTarget.dataset.type+1;
    if(!app.globalData.hasLogin){
      util.navigate("/pages/auth/login/login");
      return;
    }
    if(type === 1){
      util.navigate("/pages/issue/issueDaily/issueDaily")
    }
    if(type === 2){
      if(brand){
        util.navigate("/pages/issue/issueGoods/issueGoods?brandId="+brand.id+"&brandName="+brand.name);
      }else{
        util.navigate("/pages/brand/brandAdd/brandAdd");
      }
    }
    if(type === 3){
      if(brand){
        util.navigate("/pages/brand/brandDetail/brandDetail?id="+brand.id+"&status=1");
      }else{
        util.navigate("/pages/brand/brandAdd/brandAdd");
      }
    }
    if(type === 4){
      if(brand){
        util.navigate("/pages/brand/brandOrder/brandOrder");
      }else{
        util.navigate("/pages/brand/brandAdd/brandAdd");
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    //自定义底部导航栏高亮显示不加会导致高亮随机跳
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }

    if(app.globalData.hasLogin){
      util.request(api.UserIndex).then(function(res) {
        if (res.errno === 0) {
          that.setData({
            brand: res.data.brand
          });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})